package com.fcgzs.pay.controller;

import com.fcgzs.entity.Result;
import com.fcgzs.entity.StatusCode;
import com.fcgzs.order.feign.OrderFeign;
import com.fcgzs.order.pojo.PayLog;
import com.fcgzs.pay.service.AliPayService;
import com.fcgzs.utils.IdWorker;
import com.fcgzs.utils.TokenDecode;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: ljj
 * @Description:
 */
@RestController
@RequestMapping("/alipay")
public class AliPayController {

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private AliPayService aliPayService;


    @Autowired
    private TokenDecode tokenDecode;

    @Autowired
    private OrderFeign orderFeign;

    @GetMapping("/createNative")
    public Result<Map> createNative(){
        //获取当前登录人信息
        Map<String, String> userInfo = tokenDecode.getUserInfo();
        String username = userInfo.get("username");

        Result<PayLog> result = orderFeign.findPayLogFromRedis(username);
        PayLog payLog = result.getData();
        Map map = null;
        if (null!=payLog) {
            map = aliPayService.createNative(payLog.getOutTradeNo(), payLog.getTotalFee()+"");
        }else{
            map = new HashMap();
        }

        return new Result<>(true, StatusCode.OK,"预下单成功",map);
    }


    @GetMapping("/queryStatus")
    public Result queryStatus(String out_trade_no){
         Result result = null;
         int count = 0;
        while (true){
            Map map = aliPayService.queryStatus(out_trade_no);
            if(map==null){
                result = new Result(false,StatusCode.ERROR,"查询状态失败");
                break;
            }

            if (StringUtils.isNotEmpty((String)map.get("status"))&&((String)map.get("status")).equals("TRADE_SUCCESS")) {

                orderFeign.updateStatus(out_trade_no,(String)map.get("tradeNo"));
                result = new Result(true,StatusCode.OK,"交易支付成功");
                break;
            }

            if (StringUtils.isNotEmpty((String)map.get("status"))&&((String)map.get("status")).equals("TRADE_CLOSED")) {
                result = new Result(true,StatusCode.OK,"未付款交易超时关闭，或支付完成后全额退款");
                break;
            }

            if (StringUtils.isNotEmpty((String)map.get("status"))&&((String)map.get("status")).equals("TRADE_FINISHED")) {
                result = new Result(true,StatusCode.OK,"交易结束，不可退款");
                break;
            }
            //间隔3秒
            try {
                Thread.sleep(30000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }


            count++;

            if(count>=10){
                result = new Result(false,StatusCode.ERROR,"交易超时，请刷新页面重新生成二维码");
                break;
            }
        }
        return result;

    }



}
